package cn.sunshine.system.controller;

import cn.sunshine.basic.annotation.PreAuthorize;
import cn.sunshine.basic.utils.AjaxResult;
import cn.sunshine.basic.utils.PageList;
import cn.sunshine.system.domain.Menu;
import cn.sunshine.system.query.MenuQuery;
import cn.sunshine.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "新增或者修改菜单" ,sn = "menu:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.save(menu);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除部门" ,sn = "menu:remove")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            menuService.remove(id);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.getAjaxResult().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据Id查询" ,sn = "menu:get")
    public Menu get(@PathVariable("id")Long id)
    {
        return menuService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "查询所有" ,sn = "menu:loadAll")
    public List<Menu> loadAll(){

        return menuService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "高级查询查询所有" ,sn = "menu:queryData")

    public PageList<Menu> queryData(@RequestBody MenuQuery query)
    {
        System.out.println(menuService.queryData(query));
        return menuService.queryData(query);
    }
    @PatchMapping
    @PreAuthorize(name = "批量删除菜单数据" ,sn = "menu:batchRemove")

    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {// 调用删除方法，根据id删除
                menuService.batchRemove( ids);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");

        }
    }
/*
* @Title: findMenuTree
* @Description: 创建菜单极树接口
* @Author: Mr.Jiang
* @Version: 1.0
* @Date:  2022/7/12 20:52
* @Parameters: []
* @Return java.util.List<cn.sunshine.org.domain.Department>
*/
    @GetMapping("/findMenuTree")
    @PreAuthorize(name = "创建菜单极树接口" ,sn = "menu:findMenuTree")

    public List<Menu> findMenuTree() {
        List<Menu> findMenuTree = menuService.findMenuTree();
        findMenuTree.forEach(System.out::println);
        return findMenuTree;
    }
}
